/**
 ******************************************************************************
 * @file    
 * @author 
 * @Version 
 * @Date    
 * @brief 
 *******************************************************************************  
 **/
#include "zb32l03x_hal.h"
#include "bsp_flash.h"
#include "app_flash.h"
/*
*********************************************************************************************************
*	功能说明: 初始化
*	形    参:
*	返 回 值:
* 说    明：
*********************************************************************************************************
*/

void app_flash_init(void)
{
    fsdsetting_t setting;
    bsp_flash_init();
    app_flash_readsetting(&setting);
    if (setting.brightness == 0xff || setting.address > 324 || setting.languageselect > 1||setting.address==0)
    {
        setting.brightness = 1;
        setting.address = 1;
        setting.languageselect = 0;
        app_flash_writesetting(&setting);
    }
}
/*
*********************************************************************************************************
*	功能说明: 片选
*	形    参:
*	返 回 值:
*   说    明：
*********************************************************************************************************
*/
void app_flash_writesetting(fsdsetting_t*setting)
{
    uint8_t buff[4];
    buff[0]=setting->brightness;
    buff[1]=setting->address>>8;
    buff[2]=setting->address&0xff;
    buff[3]=setting->languageselect;
    bsp_flash_erase(); 
    bsp_flash_prog(buff, sizeof(buff));  
}


void app_flash_readsetting(fsdsetting_t*setting)
{
    uint8_t buff[4];
    bsp_flash_read(buff, sizeof(buff));
    setting->brightness=buff[0];
    setting->address=buff[1]<<8|buff[2];
    setting->languageselect=buff[3];
    setting->languageselect = 0;
}
